﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using geetol.sms.core;

namespace geetol.sms.api
{
    public partial class editSqlConnection : Form
    {
        public editSqlConnection()
        {
            InitializeComponent();
        }

        private void editSqlConnection_Load(object sender, EventArgs e)
        {
            loadSqlConnection();
        }

        void loadSqlConnection()
        {
            var s = config.LoadSqlConnection();
            if (!string.IsNullOrEmpty(s))
            {
                var strs = s.Split(';');
                if(strs.Length == 4)
                {
                    textBox1.Text = strs[0].Split('=')[1];
                    textBox2.Text = strs[1].Split('=')[1];
                    textBox3.Text = strs[2].Split('=')[1];
                    textBox4.Text = strs[3].Split('=')[1];
                }
            }
        }


        bool isdata()
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)|| 
                string.IsNullOrEmpty(textBox3.Text)||string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("填写的信息不能为空");
                return false;
            }
            return true;
        }

        private void BtnSave_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (isdata())
            {
                DbHelperSQL.connectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};PassWord={3}", textBox1.Text
                    , textBox2.Text, textBox3.Text, textBox4.Text);
                try
                {
                    DbHelperSQL.TabExists("gt_vip");
                    config.SaveSqlConnection(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
                    MessageBox.Show("连接成功，信息保存成功");
                    this.Close();
                }
                catch
                {
                    MessageBox.Show("连接失败，请确认数据库信息");
                }
            }
        }
    }
}
